package controlador;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JRadioButton;

import vista.ReporteVentas;
import vista.ReporteVentasModeloTabla;

import modelo.Orden;
import modelo.OrdenDAO;
import modelo.ReporteVentasOrdenarPorCantidad;
import modelo.ReporteVentasOrdenarPorMonto;


public class ControladorReporteVentas implements ActionListener{

	private ReporteVentas reporteVentas;
	private static String[] arreOrdenar = {"--","Por Cantidad","Por Monto"};
	private List<Orden> ordenes;

	public ControladorReporteVentas() {
		super();
		this.reporteVentas = new ReporteVentas();
		this.reporteVentas.addListener(this);
		this.reporteVentas.setLocationRelativeTo(null);
		this.reporteVentas.setVisible(true);
		this.reporteVentas.setComboOrdenar(arreOrdenar);
		ordenes = new ArrayList<Orden>();
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JComboBox){
			cambioComboOrdenar();
		}
		else if (e.getActionCommand().equals("Descendente")){
			OrdenarDescendente();
		}
		else reporteVentas.dispose();
	}

	private void OrdenarDescendente() {
		Collections.reverse(ordenes);
		reporteVentas.setTabReportes(new ReporteVentasModeloTabla(ordenes));
	}

	private void cambioComboOrdenar() {
		OrdenDAO ordenDAO = new OrdenDAO();
		ordenes = ordenDAO.consultarOrdenes();
		if (reporteVentas.getComboOrdenar().equals(arreOrdenar[1])){
			Collections.sort(ordenes, new ReporteVentasOrdenarPorCantidad());
			reporteVentas.setTabReportes(new ReporteVentasModeloTabla(ordenes));
		}
		else if (reporteVentas.getComboOrdenar().equals(arreOrdenar[2])){
			Collections.sort(ordenes, new ReporteVentasOrdenarPorMonto());
			reporteVentas.setTabReportes(new ReporteVentasModeloTabla(ordenes));
		}
	}
	
	
}
